﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Holoville.HOTween;

public partial class BattlefieldFriendScene : Scene
{
	const string BATTLEFIELD_FRIEND_CELL_NAME = "Cell - Battlefield Friend";
	
	FriendModel[] totalFriendArray = null;
	Dictionary<int, FriendModel> battlefieldFriendList = new Dictionary<int, FriendModel>();
	Dictionary<int, GameObject> battlefieldFriendObjectList = new Dictionary<int, GameObject>();

	public RecommendFriend recommendFriend;
	public UIGrid battlefieldFriendGrid;
	public GameObject battlefieldFriendCell;
	public GameObject battlefieldFriendGroup;
	public GameObject addOneFriend;
	public GameObject addTwoFriends;
	public GameObject addThreeFriends;
	public GameObject recommendFriendGroup;
	public GameObject recommendFriendNoneGroup;

	[System.NonSerialized] public List<BattlefieldFriendCheckBox> battlefieldFriendCheckBoxList = new List<BattlefieldFriendCheckBox>();

	public override void AwakeScene ()
	{
		InitData();

		recommendFriend = recommendFriendGroup.GetComponent<RecommendFriend>();
		LoadBattlefieldFriend();
	}

	public override void StartScene ()
	{

	}

	void InitData()
	{
		UserData.Instance.battleFriend = null;
		UserData.Instance.battleFriendId = 0;
		UserData.Instance.battleFriendIsMyFriend = false;
	}

	public void UncheckedOthers(BattlefieldFriendCheckBox target)
	{
		foreach (BattlefieldFriendCheckBox bfc in battlefieldFriendCheckBoxList)
		{
			if ( target.GetInstanceID() != bfc.GetInstanceID() )
				bfc.IsChecked = false;
		}
	}

	public FriendModel GetSelectedFriend()
	{
		foreach (BattlefieldFriendCheckBox bfc in battlefieldFriendCheckBoxList)
		{
			if ( bfc.IsChecked )
			{
				if (bfc.transform.parent.transform.parent.GetComponent<BattlefieldFriendCell>() != null)
				{
					int indexPath = bfc.transform.parent.transform.parent.GetComponent<BattlefieldFriendCell>().indexPath;
					return battlefieldFriendList[indexPath];
				}
				else if (bfc.transform.parent.GetComponent<RecommendFriend>() != null)
				{
					return recommendFriend.friendData;
				}
			}
		}
		return null;
	}

	public bool GetSelectedFriendIsMyFriend()
	{
		foreach (BattlefieldFriendCheckBox bfc in battlefieldFriendCheckBoxList)
		{
			if ( bfc.IsChecked )
			{
				if (bfc.transform.parent.transform.parent.GetComponent<BattlefieldFriendCell>() != null)
				{
					return true;
				}
				else if (bfc.transform.parent.GetComponent<RecommendFriend>() != null)
				{
					return false;
				}
			}
		}
		return false;
	}


	void LoadRecommendFriend()
	{
		NetworkManager.Instance.C2S_FRIEND_LIST( new int[]{ 4 }, (responseData) => {

			if (responseData.ecode != 0)
			{
				PopupUtility.OneButtonErrorPopup(responseData.ecode);
				return;
			}

			FriendModel fm = responseData.data.ToArray()[0];

			recommendFriend.friendData = fm;
			recommendFriend.nameLabel.text = fm.name.ToString();
			recommendFriend.levelLabel.text = string.Format("Lv {0}", fm.lv.ToString());
			recommendFriend.lastAccessLabel.text = DatetimeToFomattedString(fm.lastAccess).ToString();
			recommendFriend.trademarkToggle.value = false;

		});
	}

	void LoadBattlefieldFriend()
	{
		NetworkManager.Instance.C2S_FRIEND_LIST( new int[]{ 1 }, (responseData) => {

			if (responseData.ecode != 0)
			{
				PopupUtility.OneButtonErrorPopup(responseData.ecode);
				return;
			}
			
			Debug.Log ("LoadBattlefieldFriend - OnSuccess : " + responseData.data.Count);

			responseData.data = ExtractDefaultFriendList(responseData.data);

			if (responseData.data.Count < 30)
			{
				NGUITools.SetActive(recommendFriendGroup, true);
				NGUITools.SetActive(recommendFriendNoneGroup, false);
				LoadRecommendFriend();
			}
			else
			{
				NGUITools.SetActive(recommendFriendGroup, false);
				NGUITools.SetActive(recommendFriendNoneGroup, true);
			}
			
			GameObject g = null;
			int idx = 0;
			
			totalFriendArray = new FriendModel[responseData.data.Count];


			switch (responseData.data.Count)
			{
			case 0:
				Debug.Log("Count 0");
				NGUITools.SetActive(addThreeFriends, true);
				break;
			case 1:
				Debug.Log("Count 1");
				NGUITools.SetActive(addTwoFriends, true);
				break;
			case 2:
				Debug.Log("Count 2");
				NGUITools.SetActive(addOneFriend, true);
				break;
			}

			if (responseData.data.Count <= 0)
				return;

			// 랜덤한 친구를 얻기 위해서 수행하는 코드
			totalFriendArray = new FriendModel[responseData.data.Count];
			foreach (FriendModel fm in responseData.data) { totalFriendArray[idx++] = fm; }
			totalFriendArray = Shuffle(totalFriendArray);

			// 3개(가능한 최대 개수)보다 클 경우 3개로 한정하기 위한 코드
			int minValue = ( 3 < responseData.data.Count ? 3 : responseData.data.Count );

			for (int i = 0; i < minValue; i++)
			{
				g = NGUITools.AddChild(battlefieldFriendGrid.transform.gameObject, battlefieldFriendCell);
				g.name = BATTLEFIELD_FRIEND_CELL_NAME;
				
				BattlefieldFriendCell bfc = g.GetComponent<BattlefieldFriendCell>();
				
				bfc.availableCellGroup.SetActive(true);
				bfc.unavailableCellGroup.SetActive(false);
				
				FriendModel fm = totalFriendArray[i];
				
				bfc.indexPath = i;
				bfc.nameLabel.text = fm.name;
				bfc.levelLabel.text = string.Format("Lv {0}", fm.lv.ToString());
				
				battlefieldFriendObjectList.Add (i, g);
				battlefieldFriendList.Add (i, fm);
			}
			
			battlefieldFriendGrid.Reposition();
		});
	}

	/*
	 * 각각의 셀에 없는 경우 표시해 줄 경우의 코드 
	 * 
	void LoadBattlefieldFriend()
	{
		NetworkManager.Instance.C2S_FRIEND_LIST( new int[]{ 1 }, (responseData) => {

			if (responseData.ecode != 0)
			{
				PopupUtility.OneButtonErrorPopup(responseData.ecode);
				return;
			}

			Debug.Log ("LoadBattlefieldFriend - OnSuccess");

			if (responseData.data.Count < 30)
				LoadRecommendFriend();

			GameObject g = null;
			int idx = 0;

			totalFriendArray = new FriendModel[responseData.data.Count];

			if (responseData.data == null || responseData.data.Count <= 0)
			{
				battlefieldFriendGroup.SetActive(false);
				battlefieldFriendNoneGroup.SetActive(true);
				return;
			}

			totalFriendArray = new FriendModel[responseData.data.Count];

			foreach (FriendModel fm in responseData.data)
			{
				totalFriendArray[idx++] = fm;
			}

			totalFriendArray = Shuffle(totalFriendArray);

			for (int i = 0; i < 3; i++)
			{
				g = NGUITools.AddChild(battlefieldFriendGrid.transform.gameObject, battlefieldFriendCell);
				g.name = BATTLEFIELD_FRIEND_CELL_NAME;

				BattlefieldFriendCell bfc = g.GetComponent<BattlefieldFriendCell>();

				if ( i <= totalFriendArray.Length - 1 )
				{

					bfc.availableCellGroup.SetActive(true);
					bfc.unavailableCellGroup.SetActive(false);

					FriendModel fm = totalFriendArray[i];

					bfc.indexPath = i;
					bfc.nameLabel.text = fm.name;
					bfc.levelLabel.text = string.Format("Lv {0}", fm.lv.ToString());

					battlefieldFriendObjectList.Add (i, g);
					battlefieldFriendList.Add (i, fm);
				}
				else
				{
					bfc.availableCellGroup.SetActive(false);
					bfc.unavailableCellGroup.SetActive(true);
				}
			}

			battlefieldFriendGrid.Reposition();
		});
	}
	*/

	public override void HomeButton ()
	{
		UserData.Instance.raidBossId = 0;
		UserData.Instance.raidBossStartData = null;
		UserData.Instance.raidBossResultData = null;
		UserData.Instance.raidBossModel = null;
		
		UserData.Instance.expansionWarResultData = null;
		UserData.Instance.quickPvPResultData = null;
		UserData.Instance.quickPVPUserModel = null;
		
		WorldSceneManager.isSentSearch = WorldSceneManager.isSuccessSearch = false;
		WorldSceneManager.sentResearchAreaId = 0;
		UserData.Instance.researchResultData = null;
		
		StandbyScene.startMode = BattleStartMode.None;

		base.HomeButton ();
		
	}

}